/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package org.aroundme.persistency;

import java.util.List;

import org.aroundme.model.Category;
import org.aroundme.model.CategoryName;
import org.aroundme.utility.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;

public class CategoryManager {

	public CategoryManager() {
	
	}
	
	public Category getCategoryByName(CategoryName name){
		return getCategoryByName(name.toString());		
	}
	
	/**
	 * 
	 * @param categoryName
	 * @return
	 */
	public Category getCategoryByName(String categoryName){
		Category result = null;
		
		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
		s.beginTransaction();
		try{
			result = (Category) s.createQuery("from Category where name=?").setString(0, categoryName).uniqueResult();
			s.getTransaction().commit();
		}catch (HibernateException e){
			s.getTransaction().rollback();
			throw e;
		}
		
		return result;
	}
	
	/**
	 * Retrieves all categories from the DB
	 * @return
	 */
	public List<Category> retrieveAllCategories(){
		List<Category> result = null;
		
		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
		s.beginTransaction();
		try{
			Criteria criteriaCategory = s.createCriteria(Category.class);
			result = (List<Category>)criteriaCategory.list();
		}catch(HibernateException e){
			s.getTransaction().rollback();
			e.printStackTrace();
			throw e;
		}
		
		return result;
	}
	
//	public List<Place> retrieveUserPlaces(String userLoginName, int begin, int amount) {
//		//TODO try to use criteria queries
//		List<Place> result = null;
//		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
//		s.beginTransaction();
//		try{
//			Criteria cUser = s.createCriteria(User.class);
//			cUser.add(Restrictions.eq("loginName", userLoginName));
//			User u = (User) cUser.uniqueResult();
//			
//			Criteria cPlace = s.createCriteria(Place.class);
//			cPlace.add(Restrictions.eq("user", u));
//			cPlace.addOrder(Order.desc("creationDate"));
//			result = cPlace.setFirstResult(begin).setMaxResults(amount).list();
//			s.getTransaction().commit();
//		}catch(HibernateException e){
//			s.getTransaction().rollback();
//			e.printStackTrace();
//			throw e;
//		}
//		return result;
//	}
	
	
}
